package com.szj.pet.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.szj.pet.hospital.common.result.Result;
import com.szj.pet.hospital.model.hosp.Account;
import com.szj.pet.hospital.model.hosp.User;
import com.szj.pet.hospital.service.AccountService;
import com.szj.pet.hospital.service.UserService;
import com.szj.pet.hospital.vo.hosp.UserQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;

@Api(tags = "用户管理")
@RestController
@RequestMapping("admin/hosp/user")
//@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private AccountService accountService;

    //1 查询所有用户信息
    @ApiOperation(value = "获取所有用户信息")
    @GetMapping("findAll")
    public Result findAllUser() {
        //调用service的方法
        List<User> list = userService.list();
        return Result.ok(list);
    }

    //2 逻辑删除用户信息
    @ApiOperation(value = "逻辑删除用户信息")
    @DeleteMapping("{id}")
    public Result removeUser(@PathVariable Long id) {
        boolean flag = userService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //3 条件查询带分页
    @ApiOperation(value = "员工信息条件查询带分页")
    @PostMapping("findPageUser/{current}/{limit}")
    public Result findPageUser(@PathVariable long current,
                                @PathVariable long limit,
                                @RequestBody(required = false) UserQueryVo userQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<User> page = new Page<>(current, limit);
        //构建条件
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        String name = userQueryVo.getName();//名字
        String phone = userQueryVo.getPhone();//手机号码
        String userName = userQueryVo.getUserName();//用户账号

        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", userQueryVo.getName());
        }
        if (!StringUtils.isEmpty(phone)) {
            wrapper.like("phone", userQueryVo.getPhone());
        }
        if (!StringUtils.isEmpty(userName)) {
            wrapper.eq("user_name", userQueryVo.getUserName());
        }

        //调用方法实现分页查询
        IPage<User> pageUser = userService.page(page, wrapper);

        //返回结果
        return Result.ok(pageUser);
    }

    //4 添加用户信息
    @PostMapping("saveUser")
    public Result saveUser(@RequestBody User user) throws ParseException {

        //调用service
        Account account = new Account();
        if(!StringUtils.isEmpty(user.getUserName())){
            String userName = user.getUserName();
            account.setUserName(userName);
            user.setUserCode(userName);
        }
        if(!StringUtils.isEmpty(user.getUserPassword())){
            String userPassword = user.getUserPassword();
            account.setUserPassword(userPassword);
            accountService.save(account);
        }
        boolean save = userService.save(user);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //5 根据id获取用户信息
    @GetMapping("getUser/{id}")
    public Result getUser(@PathVariable Long id) {

        User user = userService.getById(id);
        return Result.ok(user);
    }
    //6 修改员工信息
    @PostMapping("updateUser")
    public Result updateUser(@RequestBody User user) {
        boolean flag = userService.updateById(user);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //7 批量删除用户信息
    @DeleteMapping("batchRemove")
    public Result batchRemoveUser(@RequestBody List<Long> idList) {
        userService.removeByIds(idList);
        return Result.ok();
    }

    //导出数据字典接口
    @GetMapping("exportData")
    public void exportDict(HttpServletResponse response) {
        userService.exportUserData(response);
    }
}
